package com.fivestar.finance.app.ui.login;


import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.fivestar.finance.app.api.UserApi;
import com.fivestar.finance.app.bean.HttpResponse;
import com.fivestar.finance.app.bean.resp.UserResponse;
import com.fivestar.finance.app.core.ExSubscriber;
import com.fivestar.finance.app.core.FFException;
import com.fivestar.finance.app.core.mvp.BasePresenter;
import com.fivestar.finance.app.greendao.UserResponseDao;
import com.fivestar.finance.app.injector.scope.ActivityScope;
import com.fivestar.finance.library.utils.SystemTool;
import com.fivestar.finance.library.utils.ToastUtils;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-13 12:55
 */
@ActivityScope
public class LoginPresenter extends BasePresenter<ILoginView> {

    private UserApi userApi;

    private UserResponseDao userDao;

    @Inject
    public LoginPresenter(UserApi userApi,UserResponseDao userDao) {
        this.userApi = userApi;
        this.userDao = userDao;
    }

    /**
     * @param userName
     * @param passWord
     */
    public void login(final String userName, final String passWord) {

        SystemTool.hideSysKeyBoard((Activity) getMvpView());

        if (TextUtils.isEmpty(userName)) {
            getMvpView().showUserNameError("请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            getMvpView().showPassWordError("请输入密码");
            return;
        }

        ExSubscriber<HttpResponse<UserResponse>> subscriber = new
                ExSubscriber<HttpResponse<UserResponse>>(this, true) {

                    @Override
                    public void onHttpError() {
                        super.onHttpError();
                        //FIXME
                        getMvpView().loginSuccess();
                    }

                    @Override
                    protected void onSuccess(Object object) {
                        UserResponse userResponse = (UserResponse) object;
                        //TODO 缓存用户信息
                        saveUserInfo();
                        ToastUtils.show((Context) getMvpView(), "登录成功", ToastUtils.LENGTH_SHORT);
                        getMvpView().loginSuccess();
                    }

                    @Override
                    protected void onFailure(FFException e) {
                        ToastUtils.show((Context) getMvpView(), "登录失败", ToastUtils.LENGTH_SHORT);
                    }
                };

        this.mCompositeSubscription.add(userApi.login("18118236518", "!@#456")
                .subscribeOn
                        (Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
//        this.mCompositeSubscription.add(userApi.login(userName, SecurityUtil.getMD5(passWord))
//                .subscribeOn
//                (Schedulers.io()).
//                observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
    }

    public UserResponse saveUserInfo() {
        UserResponse userResponse = new UserResponse();
        userResponse.setUserId("10222");
        userResponse.setMobile("18952717567");
        userResponse.setName("Derek");
        userResponse.setSex(1);
        userResponse.setPortraitPicUrl("http://v1.qzone.cc/avatar/201508/30/12/16/55e2839c3d97f155.jpg%21200x200.jpg");
        userResponse.setTokenId("s2foi239089sds09d9s09d0s9dfs0d");
        userDao.insertOrReplace(userResponse);
        return userResponse;
    }


}
